package com.kuanter.kuanterauto.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.Scroller;

import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.activity.PayNoCard;

public class CustomView5 extends LinearLayout {

	private static final String TAG = "CustomView";

	private Scroller mScroller;
	private GestureDetector mGestureDetector;
	
	LinearLayout second;
	LinearLayout first;
	Context context;
	
	public static boolean fresh = true;
	
	public CustomView5(Context context) {
		this(context, null);
	}
	
	public CustomView5(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		setClickable(true);
		setLongClickable(true);
		mScroller = new Scroller(context);
		mGestureDetector = new GestureDetector(context, new CustomGestureListener());
		
	}

	//调用此方法滚动到目标位置
	public void smoothScrollTo(int fx, int fy) {
		int dx = fx - mScroller.getFinalX();
		int dy = fy - mScroller.getFinalY();
		smoothScrollBy(dx, dy);
	}

	//调用此方法设置滚动的相对偏移
	public void smoothScrollBy(int dx, int dy) {

		//设置mScroller的滚动偏移量
		int sy = mScroller.getFinalY();
		if(sy < -PayNoCard.height + PayNoCard.JC) sy = -PayNoCard.height + PayNoCard.JC;
		mScroller.startScroll(mScroller.getFinalX(), sy, dx, dy);
		invalidate();//这里必须调用invalidate()才能保证computeScroll()会被调用，否则不一定会刷新界面，看不到滚动效果
	}
	
	@Override
	public void computeScroll() {
	
		//先判断mScroller滚动是否完成
		if (mScroller.computeScrollOffset()) {
			Log.i("uuu", mScroller.getCurrY() + "滚动。。。。。。。。。。。。。");
			//这里调用View的scrollTo()完成实际的滚动
			int sy = mScroller.getCurrY();
			if(mScroller.getCurrY() < -PayNoCard.height + PayNoCard.JC - 0.5) sy = -PayNoCard.height + PayNoCard.JC - 1;
			if(mScroller.getCurrY() > 0) sy = 0;
			scrollTo(mScroller.getCurrX(), sy);
			
			//必须调用该方法，否则不一定能看到滚动效果
			postInvalidate();
		}
		super.computeScroll();
	}
	
	float refrshHeight = 0.5f;
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
//		System.out.println(event.getY() + "getY");
		switch (event.getAction()) {
		case MotionEvent.ACTION_UP :
			Log.i(TAG, "get Sy" + getScrollY());
//			smoothScrollTo(0, 0);
			if(!fresh) {
				refrshHeight = 5f;
			} else {
				refrshHeight = 0.5f;
			}
			
			if(getScrollY() <= -PayNoCard.height + PayNoCard.JC - refrshHeight) {
//				if(getScrollY() == -MainActivity.height) {
					
					first = (LinearLayout) getChildAt(0);
					second = (LinearLayout) getChildAt(1);
					Animation a = AnimationUtils.loadAnimation(context, R.anim.slide_down_out);
					a.setFillAfter(true);
					a.setAnimationListener(new AnimationListener() {
						@Override
						public void onAnimationStart(Animation animation) {
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void onAnimationRepeat(Animation animation) {
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void onAnimationEnd(Animation animation) {
							PayNoCard.handler.sendEmptyMessage(0);
							/*second.setVisibility(View.VISIBLE);
							
							post(new Runnable() {
								@Override
								public void run() {
									smoothScrollTo(0, 0);
									second.layout(0, MainActivity.height, 0, 0);
									first.layout(0, 0, 0, 0);
									requestLayout();
								}
							});*/
//							LayoutParams lp = (LayoutParams) first.getLayoutParams();
//							lp.topMargin = -MainActivity.height;
//							LayoutParams lp1 = (LayoutParams) second.getLayoutParams();
//							second.setPadding(0, 0, 0, 0);
//							second
							/*View v1 = View.inflate(context, R.layout.v1, null);
							LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
							removeAllViews();
							lp.topMargin = -2*MainActivity.height;
							v1.setLayoutParams(lp);
							View v2 = View.inflate(context, R.layout.v2, null);
							addView(v1);
							addView(v2);*/
//						second.setVisibility(GONE);
						}
					});
				 	second.startAnimation(a);
				/*} else {
					Log.i("aaa", getScrollY() + MainActivity.height + "SSSSSSSS" + getScrollY() + "sss" + MainActivity.height);
					second = (LinearLayout) getChildAt(1);
					Animation aa = new TranslateAnimation(0, 0, -MainActivity_1.height-getScrollY(), MainActivity_1.height);
					aa.setDuration(1000);
					aa.setFillAfter(true);
					aa.setAnimationListener(new AnimationListener() {
						@Override
						public void onAnimationStart(Animation arg0) {
							
						}
						
						@Override
						public void onAnimationRepeat(Animation arg0) {
							
						}
						
						@Override
						public void onAnimationEnd(Animation arg0) {
//							Animation a = AnimationUtils.loadAnimation(context, R.anim.slide_down_out);
//							a.setFillAfter(false);
							Animation a = new TranslateAnimation(0, 0, MainActivity_1.height, 1280);
							a.setAnimationListener(new AnimationListener() {
								
								@Override
								public void onAnimationStart(Animation animation) {
									// TODO Auto-generated method stub
									
								}
								
								@Override
								public void onAnimationRepeat(Animation animation) {
									// TODO Auto-generated method stub
									
								}
								
								@Override
								public void onAnimationEnd(Animation animation) {
									MainActivity_1.handler.sendEmptyMessage(0);
								}
							});
							a.setDuration(500);
							a.setFillAfter(true);
							second.startAnimation(a);
						}
					});
					startAnimation(aa);
				}*/
			} else {
				smoothScrollTo(0, 0);
			}
			break;
		default:
			if(getScrollY() >= -PayNoCard.height + PayNoCard.JC && getScrollY() <= 0) {
				return mGestureDetector.onTouchEvent(event);
			} else {
				return true;
			}
		}
		return super.onTouchEvent(event);
	}
	
	class CustomGestureListener implements GestureDetector.OnGestureListener {

		@Override
		public boolean onDown(MotionEvent e) {
			// TODO Auto-generated method stub
			return true;
		}

		@Override
		public void onShowPress(MotionEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public boolean onSingleTapUp(MotionEvent e) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
			
			int dis = (int)((distanceY-0.5));
//			Log.i(TAG, dis + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + distanceY);
//			if(dis <= 0) {
			if(mScroller.getFinalY() > -PayNoCard.height + PayNoCard.JC - 0.5) {
				smoothScrollBy(0, dis);
			}
//			}
			return false;
		}

		@Override
		public void onLongPress(MotionEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			// TODO Auto-generated method stub
			return false;
		}
		
	}
}